﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using PhoneAppRssReader.Entity;

namespace PhoneAppRssReader
{
    public partial class DetailFeedItem : INotifyPropertyChanged
    {
        private int _indexItem;
        private List<FeedItem> _feedItems;

        private FeedItem _selectedItem;
        public FeedItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if(value==null) return;

                _selectedItem = value;
                _selectedItem.IsNew = false;
                OnPropertyChanged("SelectedItem");
            }
        }

        public DetailFeedItem()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                _feedItems = FeedDataPage.FeedData;
                _indexItem = Convert.ToInt16(NavigationContext.QueryString["index"]);
                SelectedItem = _feedItems[_indexItem];

                var prevBtn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
                var nextBtn = (ApplicationBarIconButton)ApplicationBar.Buttons[2];
                EnableApplcationaBarButton(prevBtn, nextBtn, _indexItem, _feedItems.Count);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Details feed item: " + ex.Message);
            }
        }
        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string proChanged)
        {
            if (string.IsNullOrEmpty(proChanged)) return;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(proChanged));
        }

        #endregion

        private void ViewInBrowser(object sender, EventArgs e)
        {
            var link = SelectedItem.Link;
            if (link == null) return;
            var webTask = new WebBrowserTask { Uri = link };
            webTask.Show();
        }

        private void PreviousFeed(object sender, EventArgs e)
        {
            scrollViewer.ScrollToVerticalOffset(0);
            //MessageBox.Show("Previous");
            var nextBtn = (ApplicationBarIconButton)ApplicationBar.Buttons[2];
            var prevBtn = (ApplicationBarIconButton)sender;
            _indexItem--;

            if (_indexItem < 0) _indexItem = 0;
            EnableApplcationaBarButton(prevBtn, nextBtn, _indexItem, _feedItems.Count);
            if (_feedItems == null || _indexItem >= _feedItems.Count) return;

            SelectedItem = _feedItems[_indexItem];
        }

        private void NextFeed(object sender, EventArgs e)
        {
            scrollViewer.ScrollToVerticalOffset(0);
            //MessageBox.Show("Next");
            _indexItem++;
            var prevBtn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            var nextBtn = (ApplicationBarIconButton)sender;
            EnableApplcationaBarButton(prevBtn, nextBtn, _indexItem, _feedItems.Count);
            if (_feedItems == null || _indexItem >= _feedItems.Count) return;
            SelectedItem = _feedItems[_indexItem];
        }

        private void EnableApplcationaBarButton(IApplicationBarMenuItem prevBtn, IApplicationBarMenuItem nextBtn, int index, int count)
        {
            if (index == 0)
            {
                prevBtn.IsEnabled = false;
                nextBtn.IsEnabled = true;
                return;
            }

            if (index > 0 && index < (count - 1))
            {
                prevBtn.IsEnabled = true;
                nextBtn.IsEnabled = true;
                return;
            }

            prevBtn.IsEnabled = true;
            nextBtn.IsEnabled = false;

        }

        private void GoHome(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }


    }
}